package com.karry.commons.web.storage;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.io.FilenameUtils;
import org.ponly.fs.FileSystem;
import org.ponly.fs.util.StorageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping({"/"})
public class UEditorController extends StorageController {
    public static final String ACTION_PARAM = "action";
    public static final String ACTION_CONFIG = "config";
    public static final String ACTION_UPLOAD_IMG = "ul";
    public static final String UP_IMG_PARAM = "file";

    @Autowired
    private FileSystem fileSystem;

    @RequestMapping({"ueditor/demo"})
    public String demo() {
        return "upload/ueditorDemo";
    }

    @ResponseBody
    @RequestMapping({"ueditor"})
    public Map<String, Object> invalid() {
        return ImmutableMap.of("state", "暂不支持该功能！");
    }

    @ResponseBody
    @RequestMapping(
            value = {"ueditor"},
            params = {"action=config"}
    )
    public Map<String, Object> config(HttpServletRequest request) {
        Map<String, Object> retMap = Maps.newHashMap();
        retMap.put("imageActionName", "ul");
        retMap.put("imageFieldName", "file");
        retMap.put("imageMaxSize", Integer.valueOf(3145728));
        retMap.put("imageAllowFiles", Lists.newArrayList(new String[]{".png", ".jpg", ".jpeg", ".gif", ".bmp"}));
        retMap.put("imageUrlPrefix", request.getContextPath());
        return retMap;
    }

    @ResponseBody
    @RequestMapping(
            value = {"ueditor"},
            params = {"action=ul"}
    )
    public Map<String, Object> upload(@RequestParam("file") MultipartFile multipart, @RequestParam(value = "relative",required = false,defaultValue = "1") boolean relative, HttpServletRequest request) {
        Map<String, Object> retMap = Maps.newHashMap();
        if(null != multipart && !multipart.isEmpty()) {
            try {
                String origName = multipart.getOriginalFilename();
                long size = multipart.getSize();
                String path = StorageUtil.store(fileSystem,origName, true, multipart.getInputStream(),true);
                String suffix = FilenameUtils.getExtension(origName);
                String url = "/storage/files/" + path;
                if(!relative) {
                    url = request.getContextPath() + url;
                }

                retMap.put("state", "SUCCESS");
                retMap.put("name", origName);
                retMap.put("original", origName);
                retMap.put("size", Long.valueOf(size));
                retMap.put("type", suffix);
                retMap.put("url", url);
            } catch (IOException var11) {
                retMap.put("state", "Upload Failed");
            }

            return retMap;
        } else {
            return this.invalid();
        }
    }

}
